package com.st.bottomnavigation.ui.adapter;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class MyItemDecoration extends RecyclerView.ItemDecoration {
    private final int horizontalMargin; // 水平边距（左右）  
    private final int verticalMargin;   // 垂直边距（上下），这是整个RecyclerView的外部边距  
  
    public MyItemDecoration(int horizontalMargin, int verticalMargin) {
        this.horizontalMargin = horizontalMargin;  
        this.verticalMargin = verticalMargin;  
    }  
  
    @Override  
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // 设置左右边距  
        outRect.left = horizontalMargin;  
        outRect.right = horizontalMargin;  
  
        // 设置上下外部边距  
        // 注意：这里设置的是RecyclerView的上下外部边距，而不是行与行之间的垂直距离  
        int position = parent.getChildAdapterPosition(view);  
        int itemCount = parent.getAdapter().getItemCount();  
        outRect.top = (position == RecyclerView.NO_POSITION || position == 0) ? verticalMargin : 0;  
        outRect.bottom = (position == itemCount - 1) ? verticalMargin : 0;  
    }  
}